﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RbacPermissionSystem.Application;
using RbacPermissionSystem.Application.Admins;
using RbacPermissionSystem.Application.Admins.Dto;
using RbacPermissionSystem.Domain;
using RbacPermissionSystem.Repositories;

namespace RbacPermissionSystem.WebApi.Controllers
{
    /// <summary>
    /// [xxx]C#叫特性，Attribute
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]//授权过滤器
    public class AdminController : ControllerBase
    {
        private readonly IAdminService adminService;

        public AdminController(IAdminService adminService)
        {
            this.adminService = adminService;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> RegisterAsync()
        {
            ResultDto dto = await adminService.Register(
                new Application.Admins.Dto.RegisterDto { 
             UserName = "admin123", Password = "admin"
            });
            return Ok(dto);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> CreateAsync(AdminCreateDto createDto)
        {
            ResultDto dto = await adminService.CreateAsync(createDto);
            return Ok(dto);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UpdateAsync(AdminUpdateDto updateDto)
        {
            ResultDto dto = await adminService.UpdateAsync(updateDto);
            return Ok(dto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> DeleteAsync(int AdminId)
        {
            ResultDto dto = await adminService.DeleteAsync(AdminId);
            return Ok(dto);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> ManyDeleteAsync(List<int> AdminIds)
        {
            ResultDto dto = await adminService.DeleteAsync(AdminIds);
            return Ok(dto);
        }

        /// <summary>
        /// 根据ID获取
        /// </summary>
        /// <param name="AdminId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAsync(int AdminId)
        {
            return Ok(await adminService.GetAsync(AdminId));
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> LoginAsync(LoginDto loginDto)
        {
            return Ok(await adminService.LoginAsync(loginDto));
        }

        /// <summary>
        /// Attribute--特性
        /// </summary>
        /// <param name="searchDto"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> PageAsync([FromQuery]AdminSearchDto searchDto, int PageIndex = 1, int PageSize = 10)
        {
            var list = await adminService.PageAsync(searchDto, PageIndex, PageSize);
            return Ok(new
            {
                Code = Code.Success,
                Data = new { 
                    PageData = list.Data.Item2,
                    Count = list.Data.Item1
                }
            });
        }
    }
}
